package com.miao.im.server;

import com.miao.im.core.exception.SocketServerExceptionBus;
import com.miao.im.server.session.SessionContext;
import com.miao.im.server.session.SessionManager;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/4/1 12:50
 * @Description:
 */
@Slf4j
@ChannelHandler.Sharable
public class ExceptionChannelHandler extends ChannelDuplexHandler{

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("捕获到异常信息", cause);
        SessionContext sessionContext = SessionManager.get(ctx.channel());
        Method method = SocketServerExceptionBus.invoke(sessionContext, cause);
        if (method == null) {
            ctx.channel().close();
        }
    }
}
